<template>
    <form-index
        :formConfig="formResetConfig"
        @submit="resetSubmit"
        type="edit"
        @formInputChange="formInputChange"
        labelwidth="80px"
        ref="form"
    >
        <!--  <div>这里还可以插入其它内容进行拓展</div> -->
    </form-index>
</template>

<script>
import formIndex from '@/components/customComponents/customForm/formIndex'
import {resetPwdForm} from '@/assets/form/managementCenter/systemManage/resetPwd'
import {AjaxCommon} from '@/libs/apiCommon'
import {PopupCommon} from '@/libs/popupCommon'
import {setToken} from '@/libs/util'
import {mapActions} from 'vuex'
export default {
    components: {
        formIndex
    },
    props: {
        user: {
            type: Object
        }
    },
    data() {
        return {
            formResetConfig: resetPwdForm(''),
            form: {}
        }
    },
    watch: {
        user: {
            handler(user) {
                if (user) {
                    const userinfo = user
                    let formConfigItem = this.formResetConfig
                    formConfigItem.map((t) => {
                        Object.keys(t).map((res) => {
                            if (t.fieldName == 'user_id') {
                                t.filedValue = userinfo['user_guid']
                            } else if (t.fieldName == 'LoginName') {
                                t.filedValue = userinfo['username']
                            } else if (t.fieldName == 'RealName') {
                                t.filedValue = userinfo['realname']
                            }
                        })
                    })
                    this.formResetConfig = formConfigItem
                    this.form = userinfo
                }
            },
            immediate: true
        }
    },

    methods: {
        ...mapActions(['handleLogOut']),
        async resetSubmit(d) {
            if (d.confirmPass != d.new_pass) {
                PopupCommon.notify('info', '登录密码和确认密码不一致')
                return
            }
            console.log(d)

            const self = this
            PopupCommon.confirm('确定重置密码?')
                .then(() => {
                    AjaxCommon('/api/User/ResetPass', 'post', d).then((res) => {
                        if (res?.data?.Code == 200) {
                            PopupCommon.notify('success', '重置成功，请重新登录。')
                            sessionStorage.setItem('ALLMENULIST', '')
                            localStorage.removeItem('userInfo')
                            sessionStorage.setItem('menuInfo', '')
                            setToken('')
                            localStorage.tagNaveList = ''
                            setTimeout(() => {
                                self.handleLogOut().then(() => {
                                    self.$router.replace({
                                        name: 'userLogin'
                                    })
                                })
                            }, 1000)
                        } else {
                            const msg = res?.data?.Message
                            if (msg) {
                                PopupCommon.notify('error', msg)
                            }
                        }
                    })
                })
                .catch(() => {
                    // 这里尽量写下，否则会报警告异常。
                    console.log('已取消删除')
                })
        },
        formInputChange(e) {}
    }
}
</script>
